<?php
namespace Admin\Model;
use Think\Model;

/**
* Created by hanquan.
* User Engineer,Administrator
* Date 2017/04/04
* Time 21:06
* AccountDetailsModel 账户明细模型类
*/
class AccountDetailsModel extends Model{


	private $AccountDetails = null;
	function __construct(){
		$this->AccountDetails = M('AccountDetails');
	}

	/**
	 * 根据日期搜索账户明细记录
	 * @param  int $offset 分页从第几条开始
	 * @param  int $limit  每页条数
	 * @param  string $date 日期
	 * @param  int $member_id 成员id
	 * @param  int $status 账户明细类型，0-全部记录/全部流水，1-收入记录/收入，2-支出记录/提现
	 * @param  int $audit_status 审核状态，0-未审核，1-已审核
	 * @return array    二维数组
	 */
	public function get_account_details($offset = 0,$limit = 10,$date = FALSE,$member_id=FALSE,$status=0,$audit_status=FALSE){
		$condition = array();
		if($date){
			$condition['a_date'] = array('like',$date.'%');
		}
		if($member_id){
			$condition['member_id'] = $member_id;
		}
		if($status){
			$condition['a_status'] = $status;
		}
		if($audit_status){
			$condition['a_audit_status'] = $audit_status;
		}
		return $this->AccountDetails->field('a_money,a_status,order_num,a_date,a_audit_status')
		->where($condition)->limit($offset,$limit)->order('aid desc')->select();
	}

	/**
	 * 提现数据列表
	 * @param  integer $offset       分页从第几条开始
	 * @param  integer $limit        每页条数
	 * @param  string $date 日期
	 * @param  int $type 成员类型，0-全部，1-城市合伙人，2-联盟商户，3-自有工程师，4-滴滴工程师，5-联盟商户工程师，6-联盟商户门店
	 * @return array    二维数组
	 */
	public function withdraw_cash_list($offset = 0,$limit = 10,$date = FALSE,$type=FALSE){
		$condition = array();
		if($date){
			$condition['a_date'] = array('like',$date.'%');
		}
		if($type){
			$condition['m_type'] = $type;
		}
		$condition['a_status'] = 2;
		$condition['a_audit_status'] = 0;
		return $this->AccountDetails->join('mp_member ON mp_account_details.member_id=mp_member.member_id','LEFT')
		->join('mp_city ON mp_member.city_id=mp_city.city_id','LEFT')
		->join('mp_province ON mp_city.pid=mp_province.pid','LEFT')
		->field('aid,m_type,province,city,m_name,a_date,a_money,m_money')
		->where($condition)->limit($offset,$limit)->select();
	}

	/**
	 * 插入账户明细数据
	 * @param array $data 账户明细数据
	 * @param int $aid 账户明细id
	 */
	public function set_account_details($data,$aid = FALSE){
		if(!$aid){
			return $this->AccountDetails->add($data);
		}else{
			$condition['aid'] = $aid;
			return $this->AccountDetails->where($condition)->save($data);
		}
	}

}